package com.zhiwang.planet.ui.my.withdraw

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.bumptech.glide.Glide
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.UtilPresenter
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.Toaster
import kotlinx.android.synthetic.main.wx_bind_dialog_layout.*
import kotlinx.android.synthetic.main.wx_bind_dialog_layout.view.*
import kotlinx.android.synthetic.main.wx_bind_dialog_layout.view.wx_bind_qrcode
import java.io.File
import javax.inject.Inject

class WxBindDialog(dialog: AlertDialog) {


    @Inject
    lateinit var utilPresenter: UtilPresenter


    companion object SOUL {

        lateinit var dialog: Dialog

        lateinit var ctx: Context

        lateinit var url: String
        fun create(context: Context, url: String): WxBindDialog {
            ctx = context
            this.url = url
            val dialogView = createDialogView(context)

            dialog = AlertDialog.Builder(context, R.style.WxBindAlertDialogStyle)
                    .setView(dialogView)
                    .create()

            dialog.window.setBackgroundDrawableResource(R.drawable.round_dialog_bg)
            return WxBindDialog(dialog as AlertDialog)
        }

        private fun createDialogView(context: Context): View {

            val layoutInflater = LayoutInflater.from(context)

            val rootView = layoutInflater.inflate(R.layout.wx_bind_dialog_layout, null)

            rootView.closeBtn.setOnClickListener {
                dialog.dismiss()
            }
            Glide.with(context)
                    .load(url)
                    .into(rootView.wx_bind_qrcode)

            return rootView

        }
    }


    fun show() {
        dialog.show()
        val activity = ctx as BaseActivity
        DaggerUserComponent.builder()
                .appComponent(activity.getAppComponent())
                .accountModule(AccountModule(activity))
                .autoDisposeModule(AutoDisposeModule(activity))
                .build()
                .inject(this)



        dialog.wx_bind_qrcode.setOnLongClickListener {

            utilPresenter.downLoadFile(url, object : UtilPresenter.CallBack {
                override fun onDownLoadComplete(f: File) {
                    if (f.exists()) {
                        MediaStore.Images.Media.insertImage(ctx.contentResolver,
                                f.absolutePath, f.name, null)
                        val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                        val uri = Uri.fromFile(f)
                        intent.data = uri
                        ctx.sendBroadcast(intent)
                        Toaster.message("已保存到相册")
                    }
                }

            }, "planet_qrcode_bind")
            return@setOnLongClickListener true
        }

    }
}